/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
/* $Id: draw.c 32 2007-07-13 03:08:09Z kf701.ye $ */

#include <math.h>

#include "macro.h"
#include "draw.h"

void draw_line(point_t p1, point_t p2, color_t color, draw_dot_func func)
{
	point_t p;
	int32_t x,y;
	for( x=MIN(p1.x,p2.x); x<=MAX(p1.x,p2.x); x++ )
	{
		y = (p2.y-p1.y)*(x-p1.x)/(p2.x-p1.x) + p1.y;
		p.x = x;
		p.y = y;
		func( p, color);
	}
}

void draw_triangle(point_t p1, point_t p2, point_t p3, color_t color, draw_dot_func func)
{
	point_t p[3];
	p[0] = p1;
	p[1] = p2;
	p[2] = p3;
	draw_polygon(3, p, color, func);
}

void draw_polygon(uint32_t num, point_t *array, color_t color, draw_dot_func func)
{
	int i;
	for(i=0; i<num; i++)
	{
		draw_line(array[i], array[(i+1)%num], color, func);
	}
}

void draw_circle(point_t center, uint32_t radius, color_t color, draw_dot_func func)
{
	point_t p;
	int32_t x,y,tmp;
	for(x=center.x-radius; x<=center.x+radius; x++)
	{
		p.x = x;
		tmp = sqrt(radius*radius -(x-center.x)*(x-center.x));
		p.y = center.y + tmp;
		func( p, color);
		p.y = center.y - tmp;
		func( p, color);
	}
	for(y=center.y-radius; y<=center.y+radius; y++)
	{
		p.y = y;
		tmp = sqrt(radius*radius - (y-center.y)*(y-center.y));
		p.x = center.x + tmp;
		func( p, color);
		p.x = center.x - tmp;
		func( p, color);
	}
}

void draw_parabola_x(point_t center, int32_t a, color_t color, draw_dot_func func)
{
	int32_t x;
	point_t p;
	for(x=center.x-100; x<center.x+100; x++)
	{
		p.x = x;
		p.y = (x-center.x)*(x-center.x)/a + center.y;
		func( p, color);
	}
}

void draw_parabola_y(point_t center, int32_t a, color_t color, draw_dot_func func)
{
	int32_t y;
	point_t p;
	for(y=center.y-100; y<center.y+100; y++)
	{
		p.y = y;
		p.x = (y-center.y)*(y-center.y)/a + center.x;
		func( p, color);
	}
}
